﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: Program.cs
//* Revision: 1.0
//* Author: JiratB
//* Platform: .NET Framework 4.0
//* Description: Code for generating CRM accounts for performance testing
//* 
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20130119  JiratB       - First release to Codeplex
//************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//-- CRM References --
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk;
//--------------------

//-- Solution References --
using AdaCRMUtilities;
//--------------------

//-- System References --
//using System.ServiceModel.Description;
//using System.IO;
//using System.Text.RegularExpressions;
//using System.Data;
//using System.Runtime.Serialization;
//using System.Data.Odbc;
//using System.Globalization;
//-----------------------

namespace account_generator
{
    class Program
    {
        /*
        public IOrganizationService _service;

        protected static string organization;
        protected static string crmServerName;
        protected static string crmServerPort;
        protected static string uriString;
         */

        static void Main(string[] args)
        {
            OrganizationUtils orgUtils = new OrganizationUtils(args);

            IOrganizationService crmService = orgUtils.GetOrganization();

            string accountName;
            string accountNumber;
            int numberOfEmployees;
            double revenue;
            double creditLimit;

            int i = Convert.ToInt32(args[3]);
            while (--i > 0)
            {
                Entity account = new Entity("account");

                accountName = AdaRandomUtils.RandomUpperCase(10) + " " + AdaRandomUtils.RandomUpperCase(10);
                accountNumber = AdaRandomUtils.RandomUpperCase(4) + AdaRandomUtils.RandomNumber(7);
                numberOfEmployees = AdaRandomUtils.RandomInt(5, 20000);
                revenue = numberOfEmployees * (double)AdaRandomUtils.RandomInt(300000, 9999999);
                creditLimit = revenue / AdaRandomUtils.RandomInt(5, 24);

                account.Attributes["accountnumber"] = accountNumber;
                account.Attributes["name"] = accountName;
                account.Attributes["emailaddress1"] = "random@doesnotexist.com";
                account.Attributes["telephone1"] = AdaRandomUtils.RandomPhoneNumber("+66", "2", 7);
                account.Attributes["telephone2"] = AdaRandomUtils.RandomPhoneNumber("+66", "2", 7);
                account.Attributes["fax"] = AdaRandomUtils.RandomPhoneNumber("+66", "2", 7);
                account.Attributes["websiteurl"] = "htt://www.doesnotexist.com";
                account.Attributes["creditlimit"] = (double)creditLimit;
                account.Attributes["revenue"] = (double)revenue;
                account.Attributes["numberofemployees"] = numberOfEmployees;
                //account.Attributes["vxrm_abbreviation"] = accountNumber.Substring(0, 3); //-- to be used with Jirat's Account and Contact solution only
                //account.Attributes["vxrm_iscustomer"] = true; //-- to be used with Jirat's Account and Contact solution only

                crmService.Create(account);

                #region Display The Created Account
                Console.WriteLine("[{0}]: Account \"{1}\" is created. AccountNumber = {2}, " +
                    "CreditLimit = {3}, Revenue = {4}, NumberOfEmployees = {5}", 
                    i, accountName, accountNumber, creditLimit, revenue, numberOfEmployees);
                #endregion
            }//-- end of while loop --

            Console.WriteLine(orgUtils.GetOrganization().ToString());
            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }//-- End of Main --

    }//-- End of class --
}//-- End of namespace --

//************************************************************************
// End of file
//************************************************************************